package com.springcloud.feigndemoserver1000.service.impl;

import com.springcloud.feigndemoserver1000.service.RemoteTestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;

@Slf4j
@Service
public class RemoteTestServiceImpl implements RemoteTestService {
    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }
    @Autowired
    private RestTemplate restTemplate;

    @Retryable(value = {Exception.class},exclude={IOException.class},maxAttempts=4,backoff=@Backoff(delay = 2000L,multiplier = 2))
    public String retryTest(String strParam,int integerParam){
        log.info("RemoteTestServiceretry.RemoteTestService.retryTest-START，strParam={},integerParam={}",strParam,integerParam);
        String result = restTemplate.getForObject("https://start.spring.io/actuator/info/404",String.class);
        log.info("RemoteTestServiceretry.RemoteTestService.retryTest.result={}", result);
        //拿到结果后的其他业务代码。。。。。
        return result;
    }
    @Recover
    public String recover(Exception e,String strParam,int integerparam){
        log.error("远程调用异常RemoteTestService.recover,strParam={},integerparam={}",strParam,integerparam,e);
        return "FAIL";
    }
}
